package com.bluemedia.bookproject.pages;

import com.bluemedia.stuff.Book;
import com.vaadin.data.Property;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.validator.IntegerValidator;
import com.vaadin.ui.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Marek Rucki
 * Date: 13.04.11
 * Time: 10:53
 */
public class BookEditPage extends VerticalLayout {

    VerticalLayout editLayout = new VerticalLayout();

    public BookEditPage() {
        displayTable(createBookList());
        this.addComponent(editLayout);
    }



    private void displayTable(List<Book> bookList) {
       final Table table = new Table();
       table.setSelectable(true);
       table.setContainerDataSource( new BeanItemContainer<Book>(bookList) );
       table.setHeight("100px");
       table.addListener( new Property.ValueChangeListener() {
        @Override public void valueChange(Property.ValueChangeEvent event) {
               Book selectedBook = (Book)table.getValue();
               displayBook(selectedBook);
           }
       });
       table.setImmediate(true);  // Makes the listener reacts "immediately" ;-)
       this.addComponent(table);


   }

   public List<Book> createBookList() {
       List<Book> bookList = new ArrayList<Book>();
       Book book = new Book();
       book.setTitle("Black Belt is not for Dummies");
       book.setAuthor("John Rizzo");
       book.setPrice(100);
       bookList.add(book);
       book = new Book();
       book.setTitle("Vaadin for Ninjas");
       book.setAuthor("Joonas Lehtinen");
       book.setPrice(200);
       bookList.add(book);
       book = new Book();
       book.setTitle("More Love With Vaadin");
       book.setAuthor("John Rizzo");
       book.setPrice(300);
       bookList.add(book);
       return bookList;
   }


    /** Creates Labels to display the book */
    private void displayBook(final Book book) {
        editLayout.removeAllComponents();
        editLayout.addComponent(new Label(book.getTitle()));
        editLayout.addComponent(new Label(book.getAuthor()));
        final Label priceLabel = new Label(book.getPrice()+"€");
        editLayout.addComponent(priceLabel);
        Button editButton = new Button("edit", new Button.ClickListener() {
            @Override public void buttonClick(Button.ClickEvent event) {
                editBook(book);
            }
        });
        editLayout.addComponent(editButton);
    }


    private void editBook(final Book book) {
        editLayout.removeAllComponents();

        getWindow().showNotification(
                        "Edit mode",
                        "Description",
                        Window.Notification.TYPE_HUMANIZED_MESSAGE);

        final Form form = new Form();

        form.setCaption("Book Edit");
        form.setDescription("Thank you for editing this book:");

        form.setWriteThrough(false);  // Use buffer, do not immediately write data in the book (=> form.commit() call needed)
        form.setItemDataSource( new BeanItem<Book>(book) );

        // Visual order
        form.setVisibleItemProperties(new String[]{"title", "author", "price"});

        // Validation
        form.getField("title").setRequired(true);
        form.getField("price").addValidator(new IntegerValidator("Price must be an Integer"));
        form.setImmediate(true);

        ////// Buttons
        // Have a button bar in the footer.
        HorizontalLayout buttonsLayout = new HorizontalLayout();
        buttonsLayout.setHeight("25px");
        form.setFooter(buttonsLayout);

        Button saveButton = new Button("Save", new Button.ClickListener() {
            @Override public void buttonClick(Button.ClickEvent event) {
                if (!form.isValid()) {
                    //getMainWindow().showNotification("Some values are invalid, please fix them or cancel.");
                    return;
                }

                // TODO: Reattach JPA Entity before the commit
                // (it has been detached between the call of editBook and the click on this button)

                form.commit();

                // Switch from edit to display mode.
                displayBook(book);
            }
        });
        buttonsLayout.addComponent(saveButton);
        buttonsLayout.setComponentAlignment(saveButton, Alignment.TOP_RIGHT);

        buttonsLayout.addComponent(new Button("Reset", form, "discard"));

        buttonsLayout.addComponent(new Button("Cancel", new Button.ClickListener() {
            @Override public void buttonClick(Button.ClickEvent event) {
                // Switch from edit to display mode.
                displayBook(book);
            }
        }));


        editLayout.addComponent(form);
    }

}
